//
//  UIViewController+NavItem.m
//  YiShou
//
//  Created by Kerry on 16/3/4.
//  Copyright © 2016年 FuMi. All rights reserved.
//

#import "UIViewController+NavItem.h"

#import <objc/runtime.h>

#define LeftItemImageOrTitle [UIImage imageNamed:@"nav_back"]?[UIImage imageNamed:@"nav_back"]:@"" //返回按钮(其实是导航栏的左item)可传个图片或标题
#define DefaultBarTintColor RGBA(255.0, 255.0, 255.0, 1.0) //导航栏默认背景颜色
#define DefaultTitleFont [UIFont systemFontOfSize:19.0] //导航栏默认标题字体大小
#define DefaultTitleColor [UIColor whiteColor] //导航栏默认标题字体颜色
#define DefaultItemTitleFont [UIFont systemFontOfSize:14.0] //导航栏默认按钮字体大小
#define DefaultItemTintColor [UIColor whiteColor] //导航栏默认按钮字体颜色

static const void *leftBlockKey  = &leftBlockKey;
static const void *rightBlockKey = &rightBlockKey;

@implementation UIViewController (NavItem)

- (void)configNavLeftItemWith:(voidBlock)action{
    
    [self configNavLeftItemWith:LeftItemImageOrTitle andAction:action];
}

- (void)configNavLeftItemWith:(id)object andAction:(voidBlock)action{
    [self configNavItemWith:object leftOrRight:YES withFont:nil withItemColor:nil andAction:action];
}

- (void)configNavRightItemWith:(id)object andAction:(voidBlock)action{
    [self configNavItemWith:object leftOrRight:NO withFont:nil withItemColor:nil andAction:action];
}

- (void)configNavItemWith:(id)object leftOrRight:(BOOL)left withFont:(UIFont *)font withItemColor:(UIColor *)color andAction:(voidBlock)action{
    
    NSCAssert([object isKindOfClass:[NSString class]] || [object isKindOfClass:[UIImage class]], @"the object must be class of NSString or UIImage");
    
    if ([object isKindOfClass:[UIImage class]]) {
        if (action) {
            objc_setAssociatedObject(self, left ? &leftBlockKey : &rightBlockKey,action,OBJC_ASSOCIATION_COPY_NONATOMIC);
        }
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:object style:UIBarButtonItemStyleDone target:self action:left ? @selector(pressLeft:) : @selector(pressRight:)];
        if (left){
            self.navigationItem.leftBarButtonItem = barButtonItem;
        }else{
            self.navigationItem.rightBarButtonItem = barButtonItem;
        }
    }else {
        if (color && font) {
            [self configNavigationItemString:object withFont:font withItemColor:color leftOrRight:left andAction:action];
        }else if(color){
            [self configNavigationItemString:object withFont:DefaultItemTitleFont withItemColor:color leftOrRight:left andAction:action];
        }else if (font){
            [self configNavigationItemString:object withFont:font withItemColor:nil leftOrRight:left andAction:action];
        }else{
            [self configNavigationItemString:object withFont:DefaultItemTitleFont withItemColor:nil leftOrRight:left andAction:action];
        }
    }
}


- (void)configNavigationItemString:(NSString*)text withFont:(UIFont*)font withItemColor:(UIColor *)color leftOrRight:(BOOL)left andAction:(voidBlock)action{
    NSCAssert([text isKindOfClass:[NSString class]], @"the text must be class of NSString");
    
    if (action) {
        objc_setAssociatedObject(self, left ? &leftBlockKey : &rightBlockKey,
                                 action,
                                 OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:text style:UIBarButtonItemStyleDone target:self action:left ? @selector(pressLeft:) : @selector(pressRight:)];
    NSDictionary * attributes = @{NSFontAttributeName: font};
    [barButtonItem setTitleTextAttributes:attributes forState:UIControlStateNormal];
    if (color) {
        [barButtonItem setTintColor:color];
    }else{
        [barButtonItem setTintColor:DefaultItemTintColor];
    }
    if (left) {
        self.navigationItem.leftBarButtonItem = barButtonItem;
    }else {
        self.navigationItem.rightBarButtonItem = barButtonItem;
    }
}


- (void)cancel
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)back
{
    [self.navigationController popViewControllerAnimated:NO];
}

#pragma mark -
#pragma mark -------------------- User Action ---------------------
- (void)pressLeft:(id)sender{
    voidBlock action = objc_getAssociatedObject(self, leftBlockKey);
    if (action) {
        action();
    }
}

- (void)pressRight:(id)sender{
    voidBlock action = objc_getAssociatedObject(self, rightBlockKey);
    if (action) {
        action();
    }
}

@end
